package com.maotai.llzhou.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.maotai.llzhou.bean.ProductInfo;



public class FaceUtil {
	
	public static boolean faceToken(String tokenid,String zimId,RequestConfig config,Map<String, Object> zhanghao) throws Exception{
		boolean re = false;
		String url = "https://cn-hangzhou-mgs-gw.cloud.alipay.com:443/mgw.htm";
		String para = "operationType=com.zoloz.zhub.zim.init.json&id=1&requestData=%5b%7b%22metaInfo%22%3a%22%7b%5c%22apdidToken%5c%22%3a%5c%22"+tokenid+"%5c%22%2c%5c%22appName%5c%22%3a%5c%22com.emaotai.ysapp%5c%22%2c%5c%22appVersion%5c%22%3a%5c%22"+ProductInfo.PRODUCT_VERSION+"%5c%22%2c%5c%22bioMetaInfo%5c%22%3a%5c%223.3.0%3a98304%2c0%5c%22%2c%5c%22deviceModel%5c%22%3a%5c%22HTC+Rezound%5c%22%2c%5c%22deviceType%5c%22%3a%5c%22android%5c%22%2c%5c%22osVersion%5c%22%3a%5c%226.0.1%5c%22%2c%5c%22zimVer%5c%22%3a%5c%221.0.0%5c%22%7d%22%2c%22zimId%22%3a%22"+zimId+"%22%7d%5d";
		String temp = "";
		for(int i=0;i<5;i++) {
			temp = sendHttpPostProxy(url,para,config);
            if (temp.contains("抱歉")) {
            	Thread.sleep(2000);
                continue;
            } else {
                break;
            }
        }
		 if (temp.contains("抱歉")) return false;
		 temp = temp.replace("\\", "");
		 Pattern p1 = Pattern.compile("token\":\"(.*)\",\"type\":100"); 
		 String token = "";
			Matcher m = p1.matcher(temp); 
			while(m.find()){  
				token += m.group(1);
			}
//		System.out.println(token);
		if(token==null || "".equals(token)) return re;
		//识别
		String facedata = zhanghao.get("facedata")+"";
		if(facedata.contains("$$$$")){
			facedata = facedata.replace("$$$$", token).replace("****", zimId);
		}else{
			facedata = facedata.replace("----", token).replace("****", zimId);
		}
		
		//System.out.println(facedata);
		Thread.sleep(2000);
		temp = sendHttpPostProxy(url,facedata,config);
		if (temp.contains("抱歉")) {
			System.out.println(zhanghao.get("code")+"----人脸识别不通过!!!");
			return false;
		}
		if(temp.contains("验证通过")){
			System.out.println(zhanghao.get("code")+"----人脸识别通过----");
			return true;
		}else{
			System.out.println(zhanghao.get("code")+"----人脸识别不通过!!!");
			return false;
		}
	}
	public static boolean faceTokenZLL(String apdidToken,String zimId,RequestConfig config) throws Exception{
		boolean re = false;
		String url = "https://cn-hangzhou-mgs-gw.cloud.alipay.com:443/mgw.htm";
		String para = "operationType=com.zoloz.zhub.zim.init.json&id=1&requestData=%5b%7b%22metaInfo%22%3a%22%7b%5c%22apdidToken%5c%22%3a%5c%22"+apdidToken+"%5c%22%2c%5c%22appName%5c%22%3a%5c%22com.emaotai.ysapp%5c%22%2c%5c%22appVersion%5c%22%3a%5c%22"+ProductInfo.PRODUCT_VERSION+"%5c%22%2c%5c%22bioMetaInfo%5c%22%3a%5c%223.3.0%3a98304%2c0%5c%22%2c%5c%22deviceModel%5c%22%3a%5c%22HTC+Rezound%5c%22%2c%5c%22deviceType%5c%22%3a%5c%22android%5c%22%2c%5c%22osVersion%5c%22%3a%5c%226.0.1%5c%22%2c%5c%22zimVer%5c%22%3a%5c%221.0.0%5c%22%7d%22%2c%22zimId%22%3a%22"+zimId+"%22%7d%5d";
		String temp = "";
		for(int i=0;i<5;i++) {
			temp = sendHttpPostProxy(url,para,config);
            if (temp.contains("抱歉")) {
            	Thread.sleep(2000);
                continue;
            } else {
                break;
            }
        }
		 if (temp.contains("抱歉")) return false;
		 temp = temp.replace("\\", "");
		 Pattern p1 = Pattern.compile("token\":\"(.*)\",\"type\":100"); 
		 String token = "";
			Matcher m = p1.matcher(temp); 
			while(m.find()){  
				token += m.group(1);
			}
		System.out.println(token);
		if(token==null || "".equals(token)) return re;
		//识别
		temp = sendHttpPostProxy(url,"",config);
		if (temp.contains("抱歉")) return false;
		if(temp.contains("验证通过")) return true;
		return re;
	}
	public static boolean faceTokenWT(String apdidToken,String zimId,RequestConfig config) throws Exception{
		boolean re = false;
		String url = "https://cn-hangzhou-mgs-gw.cloud.alipay.com:443/mgw.htm";
		String para = "operationType=com.zoloz.zhub.zim.init.json&id=1&requestData=%5b%7b%22metaInfo%22%3a%22%7b%5c%22apdidToken%5c%22%3a%5c%22"+apdidToken+"%5c%22%2c%5c%22appName%5c%22%3a%5c%22com.emaotai.ysapp%5c%22%2c%5c%22appVersion%5c%22%3a%5c%22"+ProductInfo.PRODUCT_VERSION+"%5c%22%2c%5c%22bioMetaInfo%5c%22%3a%5c%223.3.0%3a98304%2c0%5c%22%2c%5c%22deviceModel%5c%22%3a%5c%22HTC+Rezound%5c%22%2c%5c%22deviceType%5c%22%3a%5c%22android%5c%22%2c%5c%22osVersion%5c%22%3a%5c%226.0.1%5c%22%2c%5c%22zimVer%5c%22%3a%5c%221.0.0%5c%22%7d%22%2c%22zimId%22%3a%22"+zimId+"%22%7d%5d";
		String temp = "";
		for(int i=0;i<5;i++) {
			temp = sendHttpPostProxy(url,para,config);
            if (temp.contains("抱歉")) {
            	Thread.sleep(2000);
                continue;
            } else {
                break;
            }
        }
		 if (temp.contains("抱歉")) return false;
		 temp = temp.replace("\\", "");
		 Pattern p1 = Pattern.compile("token\":\"(.*)\",\"type\":100"); 
		 String token = "";
			Matcher m = p1.matcher(temp); 
			while(m.find()){  
				token += m.group(1);
			}
		System.out.println(token);
		if(token==null || "".equals(token)) return re;
		//识别
		temp = sendHttpPostProxy(url,"",config);
		if (temp.contains("抱歉")) return false;
		if(temp.contains("验证通过")) return true;
		return re;
	}
	public static String sendHttpPostProxy(String httpUrl, String para,RequestConfig config) throws Exception {  
        HttpPost httpPost = new HttpPost(httpUrl);
        httpPost = setHttpPostHeader(httpPost);
        httpPost.setEntity(new StringEntity(para)); 
//        httpPost.setConfig(config);
        return sendHttpPostProxy(httpPost);  
    }
	private static HttpPost setHttpPostHeader(HttpPost httpPost) throws Exception {
		httpPost.addHeader("uuid","52a074e0-2dce-4b54-b7f9-2d34c35521af");
		httpPost.addHeader("WorkspaceId", "prod");
		httpPost.addHeader("AppId", "C321516081430");
		httpPost.addHeader("Accept-Encoding", "gzip");
		httpPost.addHeader("Connection", "Keep-Alive");
		httpPost.addHeader("cookie","zone=RZ13A; cna=OMsOFPlcmRQCAXnlvUJt3VsG");
		httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
		httpPost.addHeader("Host","cn-hangzhou-mgs-gw.cloud.alipay.com:443");
		httpPost.addHeader("User-Agent","android");
		return httpPost;
	}
	private static String sendHttpPostProxy(HttpPost httpPost) throws Exception{  
        HttpClient httpClient = null;  
        HttpResponse response = null;  
        HttpEntity entity = null;
		String return_str = null;
        httpClient = HttpClients.createDefault(); 
        response = httpClient.execute(httpPost); 
        entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
//		System.out.println(return_str);
        
        return return_str;  
    } 
}
